package com.ktjy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ktjy.dto.DictTypeDTO;
import com.ktjy.pojo.SysDictType;
import com.ktjy.service.ISysDictTypeService;
import com.ktjy.utils.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-08-17
 */
@RestController
@RequestMapping("/api/sys/dictType")
@Slf4j
public class SysDictTypeController {
    @Autowired
    ISysDictTypeService sysDictTypeService;

    @GetMapping("/list")
    public AjaxResult list(DictTypeDTO dictTypeDTO,
                           @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        IPage<SysDictType> page = sysDictTypeService.findPage(dictTypeDTO, pageNo, pageSize);
        return AjaxResult.success(page);
    }

    @PostMapping("/save")
    public AjaxResult saveOrUpdate(@RequestBody SysDictType sysDictType){
        try {
            boolean ok=sysDictTypeService.saveOrUpdate(sysDictType);
            if(ok){
                return AjaxResult.success("保存成功");
            }
        } catch (Exception e) {
            log.error("SysDictTypeController中saveOrUpdate方法执行报错的。",e);
        }
        return AjaxResult.error("保存失败");
    }

    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id){
        //查询用户详情
        try {
            SysDictType sysDictType=sysDictTypeService.getById(id);
            if(sysDictType!=null){
                return AjaxResult.success(sysDictType);
            }
        } catch (Exception e) {
            log.error("SysDictTypeController中view方法执行报错的。",e);
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/del/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            boolean ok= sysDictTypeService.removeById(id);
            if(ok){//删除成功
                return AjaxResult.success("删除成功");
            }
        } catch (Exception e) {
            log.error("SysDictTypeController中del方法执行报错的。",e);
        }
        return AjaxResult.error("删除失败");
    }
}
